import 'package:flutter/material.dart';
import 'package:wanchong/path.dart';
import 'package:wanchong/utils/common.dart';
import 'package:wanchong/widget/app_bar.dart';
import 'package:wanchong/utils/upload.dart';
import 'dart:io';
import 'package:path_provider/path_provider.dart';

class SettingHome extends StatefulWidget {
  @override
  _SettingHomeState createState() => _SettingHomeState();
}

class _SettingHomeState extends State<SettingHome> {
  double _tmpSized = 0.0;
  @override
  void initState() {
    _getCache();
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: MyAppBar(
        child: Stack(
          children: <Widget>[
            Center(
              child: Text(
                "设置",
                style: TextStyle(
                    color: Color(0xFF3C4450),
                    fontWeight: FontWeight.w700,
                    fontSize: 18.0),
              ),
            ),
            GestureDetector(
              onTap: () {
                // print("object");
                Navigator.of(context).pop();
              },
              child: Container(
                padding: EdgeInsets.only(right: 15.0),
                color: Colors.transparent,
                height: double.infinity,
                child: Image.asset("assets/login/top_return_icon.png"),
              ),
            )
          ],
        ),
      ),
      body: Container(
        height: double.infinity,
        child: Column(
          // color: Colors.white,
          children: [
            Expanded(
              child: Container(
                // color: Colors.teal,
                // height: double.infinity,
                child: ListView(
                  physics: BouncingScrollPhysics(),
                  children: <Widget>[
                    SizedBox(
                      height: 15.0,
                      // color: Color(0xFAFAFAFA),
                    ),
                    _customItem("账号与安全", () {}),
                    // _customItem("清理缓存", () {}),
                    _clearItem("清理缓存"),
                    SizedBox(
                      height: 15.0,
                    ),
                    _customItem("欢迎评分", () {}),
                    _customItem("关于萌宠管家", () {
                      Navigator.of(context).pushNamed(AboutPath);
                    }),
                    _customItem("意见反馈", () {
                      Navigator.of(context).pushNamed(FeedBackPath);
                    }),
                  ],
                ),
              ),
            ),
            GestureDetector(
              onTap: () async {
                ///退出登录
                await quit();
              },
              child: Container(
                height: 50.0,
                width: double.infinity,
                alignment: Alignment.center,
                color: Colors.white,
                child: Text(
                  "退出登录",
                  style: TextStyle(fontSize: 14.0, color: Color(0xFFFF5C64)),
                ),
              ),
            ),
            Container(
              height: 15.0,
              color: Colors.white,
            ),
          ],
        ),
      ),
    );
  }

  Widget _customItem(title, callback) {
    return Container(
      color: Colors.white,
      padding: EdgeInsets.only(left: 15.0),
      child: Column(
        children: <Widget>[
          GestureDetector(
            onTap: () {
              callback();
            },
            child: Container(
              color: Colors.transparent,
              padding: EdgeInsets.only(right: 15.0, top: 24.0, bottom: 24.0),
              child: Row(
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                children: <Widget>[
                  Text(
                    title,
                    style: TextStyle(color: Color(0xFF3C4450), fontSize: 14.0),
                  ),
                  Icon(
                    Icons.chevron_right,
                    color: Color(0xFFB4BEC6),
                    size: 20.0,
                  )
                ],
              ),
            ),
          ),
          Divider(
            color: Color(0xFFDBDBDB),
            height: 1.0,
          )
        ],
      ),
    );
  }

  ///清理缓存标签
  Widget _clearItem(title) {
    return Container(
      color: Colors.white,
      padding: EdgeInsets.only(left: 15.0),
      child: Column(
        children: <Widget>[
          Container(
            // color: Colors.transparent,
            padding: EdgeInsets.only(right: 15.0, top: 24.0, bottom: 24.0),
            child: Row(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              children: <Widget>[
                Text(
                  title,
                  style: TextStyle(color: Color(0xFF3C4450), fontSize: 14.0),
                ),
                Row(
                  children: <Widget>[
                    Container(
                      padding: EdgeInsets.only(left: 5, right: 5),
                      child: Text(
                        renderSize(_tmpSized),
                        style:
                            TextStyle(color: Color(0xFF3C4450), fontSize: 14.0),
                      ),
                    ),
                    _tmpSized > 0.0
                        ? Container(
                            // padding: EdgeInsets.only(left: 5, right: 5),
                            child: Text(
                              "|",
                              style: TextStyle(
                                  color: Color(0xFF3C4450), fontSize: 14.0),
                            ),
                          )
                        : Container(),
                    _tmpSized > 0.0
                        ? GestureDetector(
                            onTap: () {
                              _clearCache();
                            },
                            child: Container(
                              padding: EdgeInsets.only(left: 5, right: 5),
                              child: Text(
                                "立即清理",
                                style: TextStyle(
                                    color: Color(0xFFFFCC33), fontSize: 14.0),
                              ),
                            ),
                          )
                        : Container()
                  ],
                )
              ],
            ),
          ),
          Divider(
            color: Color(0xFFDBDBDB),
            height: 1.0,
          )
        ],
      ),
    );
  }

  quit() async {
    await removeToken();
    Navigator.pushNamedAndRemoveUntil(
        context, LoginPath, (Route<dynamic> route) => false);
  }

  _getCache() async {
    var c = await FileUtils.getInstance().loadCache();
    setState(() {
      _tmpSized = c;
    });
    // var tmp = renderSize(c);
    // print(tmp);
  }

  void _clearCache() async {
    //此处展示加载loading
    try {
      Directory tempDir = await getTemporaryDirectory();
      //删除缓存目录
      await FileUtils.getInstance().delDir(tempDir);
      await _getCache();
      alert('清除缓存成功');
    } catch (e) {
      print(e);
      // ToastUtils.show(msg: '清除缓存失败');
      alert('清除缓存失败');
    } finally {
      //此处隐藏加载loading
    }
  }
}
